package com.atguigu.regex.regex;

import java.awt.image.RGBImageFilter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 张晓
 * @create 2020-10-29 9:59
 */
public class RegexMain {
    public static void main(String[] args) {

        //1. \s+ 可以匹配多个空格
        String str = "{我   是}";
        String regex = "\\{我\\s+是\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo1 Found value:"+ matcher.group(0));
        }

        /**
         * 2. ^\d+(\.\d+)?
         *
         * ^ 定义了以什么开始
         *
         * \d+ 匹配一个或多个数字
         *
         * ? 设置括号内的选项是可选的
         *
         * \. 匹配 "."
         *
         * 可以匹配的实例："5", "1.5" 和 "2.21"。
         */

        str = "6789999.1";
        regex = "\\d+(\\.\\d+)?";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo2 Found value:"+ matcher.group(0));
        }


        str = "I am noob from runoob.com.";
        regex = ".*runoob.*";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo3 Found value:"+ matcher.group(0));
        }

        //按指定模式在字符串查找
        str = "This order was placed for QT3000! OK?";
        regex = "(.*)";
        //1.(\D*)(\d+)(.*) √
        //2.(\D*) √
        //3.(\d+) √
        //4.(.*)

//        Demo4 Found value:This order was placed for QT3000! OK?
//        Demo4 Found value:This order was placed for QT
//        Demo4 Found value:3000
//        Demo4 Found value:! OK?
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo4 Found value:"+ matcher.group(0));
//            System.out.println("Demo4 Found value:"+ matcher.group(1));
//            System.out.println("Demo4 Found value:"+ matcher.group(2));
//            System.out.println("Demo4 Found value:"+ matcher.group(3));
        }else {
            System.out.println(" no match ");
        }


        /**
         * x|y
         * 匹配 x 或 y。例如，'z|food' 匹配"z"或"food"。'(z|f)ood' 匹配"zood"或"food"。
         */
        str = "food zood";
        regex = "(?:z|f)ood";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo5 Found value:"+ matcher.group(0));
        }else {
            System.out.println(" no match ");
        }

        /**
         * [xyz] 字符集。匹配包含的任一字符。例如，"[abc]"匹配"plain"中的"a"。
         */
        str = "plain";
        regex = "[abc]";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo6 Found value:"+ matcher.group(0));
        }else {
            System.out.println(" no match ");
        }


        str = "中通评报字〔20**〕**号";
        regex = "中通评报字〔[0-9*]{4}〕.*号";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo7 Found value:"+ matcher.group(0));
        }else {
            System.out.println(" no match ");
        }

        //[○一二三四五六七八九×]{4}年

        //((××月××日)|((○[一三五七八]|(一○|十二))月(○[一二三四五六七八九]|[十二][○一二三四五六七八九]|三[○一])日)|
        //(○二月(○[一二三四五六七八九]|[十二][○一二三四五六七八九])日)|
        //((○[四六九]|十一)月(○[一二三四五六七八九]|[十二][○一二三四五六七八九]|三○)日))
        str = "二○二○年○三月十八日";
        regex = "[○一二三四五六七八九×]{4}年[○一二三四五六七八九十×]{2}月[○一二三四五六七八九十×]{2}日";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo8 Found value:"+ matcher.group(0));
        }else {
            System.out.println(" no match ");
        }


        //评估基准日
        str = "{评估基准日8sfs号起，至9号fsf止";
        regex = "[{]?评估基准日.*起[,，]至.*止[}]?";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo9 Found value:"+ matcher.group(0));
        }else {
            System.out.println(" no match ");
        }

        //××法
        str = "收益法";
        regex = "××法|资产基础法|收益法|市场法";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo10 Found value:"+ matcher.group(0));
        }else {
            System.out.println(" no match ");
        }

        //××法和××法
        str = "××法和××法";
        regex = "(××法|资产基础法|收益法|市场法)和{0,1}(××法|资产基础法|收益法|市场法)";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo11 Found value:"+ matcher.group(0));
        }else {
            System.out.println(" no match ");
        }

        /**
         * [{]?[0-9\*]{4}年
         * ((\*\*月\*\*日)|((0[13578]|(10|12))月(0[1-9]|[1-2][0-9]|3[0-1])日)|(02月(0[1-9]|[1-2][0-9])日)|((0[469]|11)月(0[1-9]|[1-2][0-9]|30)日))[}]?
         */
        str = "20**年**月**日";
        regex = "[{]?[0-9\\*]{4}年(" +
                "(\\*\\*月\\*\\*日)|((0[13578]|(10|12))月(0[1-9]|[1-2][0-9]|3[0-1])日)|(02月(0[1-9]|[1-2][0-9])日)|((0[469]|11)月(0[1-9]|[1-2][0-9]|30)日)" +
                ")[}]?";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println("Demo11 Found value:"+ matcher.group(0));
        }else {
            System.out.println(" no match ");
        }


        /**
         * <span\s[^>]*style="(([^>]*[^a-zA-Z0-9-_]color)|(color))\s*:\s*((red)|(rgb\(255,\s*0,\s*0\)))[^>]*">(((【[^】]*】)+)|((<span\s[^>]*>【[^】]*】</span>)+)|(<span\s[^>]*>(【[^】]*】)+</span>))</span>
         */


    }
}
